﻿using System;
using System.Collections.Generic;
using ZyGames.DZPocker.Model;
using ZyGames.Framework.Cache.Generic;
using ZyGames.Framework.Game.Cache;
using ZyGames.Framework.Game.Context;
using ZyGames.Framework.Game.Contract;
using ZyGames.Framework.Game.Contract.Action;
using ZyGames.Framework.Game.Lang;
using ZyGames.Framework.Game.Service;
using System.Linq;
using ZyGames.DZPocker.Bll.Logic;
using ZyGames.DZPocker.Script;
using System.Text.RegularExpressions;
using ZyGames.Framework.Net;
using ZyGames.Framework.Common.Serialization;
using ZyGames.DZPocker.Tools;

namespace ZyGames.DZPocker.Script.CsScript.Action
{
	/// <summary>
	/// 申请好友详细信息
	/// </summary>
	public class Action20040 : BaseAction
	{
		private int userId;
		private GameUser _currentGameUser;
		private string simpleUserDataStr;
		private string signature;
		private string maxCardType;
		private string currentPlayTableStr;
		private List<TableData> tableData;
		private string winOpps;
		private string allInOpps;
		public Action20040(HttpGet actionGetter)
			: base((short)ActionType.DZApplyFriendsDetailInfo, actionGetter)
		{
		}
		public override bool GetUrlElement()
		{
			if (httpGet.GetInt("UserId", ref userId))
			{
				return true;
			}
			return false;
		}

		public override bool TakeAction()
		{
			_currentGameUser = GameTable.Current.GetUser(userId);
			if (_currentGameUser == null)
			{
				Tool.WriteWarmingLine("消息20040玩家不存在");
				return false;
			}
			tableData = new List<TableData>();
			SimpleUserData userData = new SimpleUserData() { NickName = _currentGameUser.GetNickName(), UserId = userId, HeadIcon = _currentGameUser.HeadIcon };
			this.simpleUserDataStr = Convert.ToBase64String(ProtoBufUtils.Serialize(userData));
			this.signature = "德州小强";
			this.maxCardType = "同花顺";
			var tableIds = _currentGameUser.Property.TableIdList;
			if (tableIds.Count > 0)
			{
				foreach (var tableId in tableIds)
				{
					var table = GameRoom.Current.GetTableData(tableId);
					if (table != null)
					{
						if (!tableData.Contains(table))
							tableData.Add(table);
					}
				}
			}
			if (tableData.Count <= 0)
				Tool.WriteErrorLine("20040没有玩家桌子信息");

			currentPlayTableStr = Convert.ToBase64String(ProtoBufUtils.Serialize(tableData));
			//玩家的胜率和AllIn概率
			this.winOpps = "48.75%";
			this.allInOpps = "5.7%";
			return true;
		}

		public override void BuildPacket()
		{
			this.PushIntoStack(this.simpleUserDataStr);
			this.PushIntoStack(this.signature);
			this.PushIntoStack(this.maxCardType);
			this.PushIntoStack(this.currentPlayTableStr);
			this.PushIntoStack(this.winOpps);
			this.PushIntoStack(this.allInOpps);
		}
	}
}
